package BlockSearch;

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        int []arr = {16,5,9,21,18,
                     32,23,37,26,45,34,
                     50,48,61,52,73,66};
        block b1 = new block(0,4,21);
        block b2 = new block(5,10,45);
        block b3 = new block(11,16,73);
        block []blocks = {b1,b2,b3};
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入查找的数字");
            int num = sc.nextInt();

            int index = getIndex(arr,blocks,num);
            System.out.println(index);
        }
    }

    private static int getIndex(int[] arr, block[] blocks, int num) {
        int FindIndex = findIndex(blocks,num);
        if(FindIndex == -1){
            return -1;
        }


        for (int i = blocks[FindIndex].getStartIndex(); i <= blocks[FindIndex].getEndIndex(); i++) {
            if(num == arr[i]){
                return i;
            }
        }

        return -1;
    }
    private static int findIndex(block[] blocks,int num){
        for (int i = 0; i < blocks.length; i++) {
            if(num <= blocks[i].getMaxNum()){
                return i;
            }
        }
        return -1;
    }
}

class block {
    private int startIndex;
    private int endIndex;
    private int maxNum;

    public block() {
    }

    public block(int startIndex, int endIndex, int maxNum) {
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.maxNum = maxNum;
    }

    /**
     * 获取
     * @return startIndex
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * 设置
     * @param startIndex
     */
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    /**
     * 获取
     * @return endIndex
     */
    public int getEndIndex() {
        return endIndex;
    }

    /**
     * 设置
     * @param endIndex
     */
    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    /**
     * 获取
     * @return maxNum
     */
    public int getMaxNum() {
        return maxNum;
    }

    /**
     * 设置
     * @param maxNum
     */
    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    public String toString() {
        return "block{startIndex = " + startIndex + ", endIndex = " + endIndex + ", maxNum = " + maxNum + "}";
    }
}

